package com.actionpals.components.controls.calendar
{
	import com.actionpals.components.controls.Calendar;
	import com.actionpals.events.CalendarEvent;
	import com.actionpals.graphics.Gradient;
	import com.actionpals.graphics.RectangleShape;
	import com.actionpals.graphics.TabShape;
	
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class CalendarDateHeader extends Sprite
	{
		private var calendar:Calendar;
		private var bg:TabShape;
		private var border:RectangleShape;
		private var nextMonthButton:DisplayObject;
		private var previousMonthButton:DisplayObject;
		private var title:TextField;
		
		public function CalendarDateHeader( calendar:Calendar )
		{
			super();
			
			this.calendar = calendar;
			
			init();
		}
		
		public function drawNow():void
		{
			draw();
		}
		
		protected function init():void
		{
			create();
			draw();
		}
		
		protected function create():void
		{
			createBG();
			createBorder();
			createTitle();
		}
		
		protected function createBG():void
		{
			bg = new TabShape( 100, 20 );
			addChild( bg );
		}
		
		protected function createBorder():void
		{
			border = new RectangleShape( 100, 1 );
			addChild( border );
		}
		
		protected function createTitle():void
		{
			title = new TextField();
			title.height = 10;
			addChild( title );
		}
		
		protected function draw():void
		{
			bg.cornerRadius = calendar.style.cornerRadius;
			bg.width = calendar.width - calendar.style.borderThickness * 2;
			bg.height = ( calendar.style.dateHeaderStyle.percentHeight / 100 ) * calendar.height;
			var matrix:Matrix = new Matrix();
			var degrees:Number = 90;
			var radians:Number = ( degrees / 180 ) * Math.PI;
			matrix.createGradientBox( bg.width, bg.height, radians );
			bg.fillGradient = new Gradient( GradientType.LINEAR, calendar.style.dateHeaderStyle.colors, calendar.style.dateHeaderStyle.alphas, [ 0, 255 ], matrix );
			bg.x = bg.y = calendar.style.borderThickness;
			bg.drawNow();
			
			border.width = calendar.width - calendar.style.borderThickness * 2;
			border.height = calendar.style.dateHeaderStyle.borderThickness;
			border.fillColor = calendar.style.dateHeaderStyle.borderColor;
			border.fillAlpha = calendar.style.dateHeaderStyle.borderAlpha;
			border.x = calendar.style.borderThickness;
			border.y = bg.height - border.height;
			border.drawNow();
			
			if( previousMonthButton != calendar.style.previousMonthButton )
			{
				if( previousMonthButton )
				{
					previousMonthButton.removeEventListener( MouseEvent.CLICK, onPreviousClick );
					removeChild( previousMonthButton );
				}
				previousMonthButton = calendar.style.previousMonthButton;
				addChild( previousMonthButton );
				previousMonthButton.addEventListener( MouseEvent.CLICK, onPreviousClick );
			}
			previousMonthButton.x = 10;
			previousMonthButton.y = height/2 - previousMonthButton.height/2 - border.height/2;
			if( nextMonthButton != calendar.style.nextMonthButton )
			{
				if( nextMonthButton )
				{
					nextMonthButton.removeEventListener( MouseEvent.CLICK, onNextClick );
					removeChild( nextMonthButton );
				}
				nextMonthButton = calendar.style.nextMonthButton;
				addChild( nextMonthButton );
				nextMonthButton.addEventListener( MouseEvent.CLICK, onNextClick );
			}
			nextMonthButton.x = width - nextMonthButton.width - 10;
			nextMonthButton.y = height/2 - nextMonthButton.height/2 - border.height/2;
			
			title.text = calendar.monthNames[ calendar.displayedMonth ] + " " + calendar.displayedYear;
			title.autoSize = TextFieldAutoSize.CENTER;
			title.embedFonts = calendar.style.dateHeaderStyle.embedFonts;
			title.selectable = false;
			title.setTextFormat( calendar.style.dateHeaderStyle.textFormat );
			title.x = width/2 - title.width/2;
			title.y = height/2 - title.textHeight/2 - border.height/2;
		}
		
		private function onNextClick( evt:MouseEvent ):void
		{
			dispatchEvent( new CalendarEvent( CalendarEvent.NEXT_MONTH_SELECT, null ) );
		}
		
		private function onPreviousClick( evt:MouseEvent ):void
		{
			dispatchEvent( new CalendarEvent( CalendarEvent.PREVIOUS_MONTH_SELECT, null ) );
		}
		
	}
}